package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.CustomerClass;

/**
 * A class used for managing customer class
 */
public class CustomerClassDAO {

	/** a database of this customer_class */
	private Database db;

	/**
	 * Instantiate a new object of this class
	 * 
	 * @param database a database
	 */
	public CustomerClassDAO(Database db) {
		this.db = db;
	}

	/**
	 * a method for searching all customer_class in database
	 * @return all customer_class
	 */
	public List<Map<String, Object>> findAll() {
		String vaSql = "SELECT * FROM customer_class ORDER BY code ASC";
		List<Map<String, Object>> result = db.queryList(vaSql);
		if (result != null) {
			return result;
		} else {
			return null;
		}
	}

	/**
	 * a method for searching customer_class by site_id
	 * @return all customer_class in specific site_id
	 */
	public List<Map<String, Object>> findCustomerClassBySiteId(int pnSiteId) {
		String vaSql = "SELECT code, description FROM customer_class WHERE site_id = ? ORDER BY code ASC";
		List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
		if (result != null) {
			return result;
		} else {
			return null;
		}
	}

	public String findDescriptionBySiteAndCode(int pnSiteId, String paCode) {
		String vaSql = "SELECT description FROM customer_class WHERE site_id = ? and code = ?";
		Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paCode);
		if (result != null) {
			return (String) result.get("DESCRIPTION");
		} else {
			return null;
		}
	}

	/**
	 * CustomerClass a method for adding a customerclass into database
	 * @param poCode ZONE to add
	 */
	public void addCustomerClass(CustomerClass poCustomerClass) {
		String vaSql = "INSERT INTO CUSTOMER_CLASS (SITE_ID,CODE,DESCRIPTION,INSERT_USER_ID,INSERT_DATE,INSERT_TIME,"
				+ "UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME)"
				+ "VALUES(?,?,?,?,?,?,?,?,?)";
		try {
			db.insert(vaSql, poCustomerClass.getVnSiteId(),
					poCustomerClass.getVaCode(),
					poCustomerClass.getVaDescription(),
					poCustomerClass.getVnInsertUserId(),
					poCustomerClass.getVaInsertDate(),
					poCustomerClass.getVaInsertTime(),
					poCustomerClass.getVnUpdateUserId(),
					poCustomerClass.getVaUpdateDate(),
					poCustomerClass.getVaUpdateTime());

		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * a method for updating a zone into database
	 * 
	 * @param pnSiteId site that CustomerClass live
	 * @param pnCodeId that will update
	 * @param poNewDescription that will update
	 */
	public void updateZone(int pnSiteId, String paCodeId,
			CustomerClass poNewCustomerClass) {
		// String vaSql =
		// "UPDATE ZONE SET ZONE_NAME = ? ,UPDATE_USER_ID=?,UPDATE_DATE=?,UPDATE_TIME=?"
		// + "WHERE SITE_ID=? and ZONE_ID=?";

		String vaSql = " UPDATE CUSTOMER_CLASS SET " + " CODE = ? , "
				+ " DESCRIPTION= ? , " + " UPDATE_USER_ID = ? , "
				+ " UPDATE_DATE = ? , " + " UPDATE_TIME = ? " + " WHERE "
				+ " CODE  = ? " + " AND SITE_ID = ? ";

		try {
			db.update(vaSql, poNewCustomerClass.getVaCode(),
					poNewCustomerClass.getVaDescription(),
					poNewCustomerClass.getVnUpdateUserId(),
					poNewCustomerClass.getVaUpdateDate(),
					poNewCustomerClass.getVaUpdateTime(), paCodeId, pnSiteId);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * delete zone by site_id, code
	 * 
	 * @param pnSiteId is an site_id that want to delete data
	 * @param pnCode is an pnCodeId that want to delete data
	 * @return true if success, false if fail
	 */
	public boolean deleteCustomerClassByAccount(int pnSiteId, String paCode) {
		String vaSql = "DELETE FROM CUSTOMER_CLASS WHERE SITE_ID = ? and CODE = ?";
		try {
			db.update(vaSql, pnSiteId, paCode);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// get Code from CustomerId check dupicate for insert
	public String findCodeBySiteAndCode(int pnSiteId, String paCode) {
		String vaSql = "SELECT code FROM customer_class WHERE site_id = ? and code = ?";
		Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paCode);
		if (result != null) {
			return (String) result.get("CODE");
		} else {
			return null;
		}
	}

	// get Code from CustomerId check dupicate for update
	public String findsCodeBySiteAndCode(int pnSiteId, String paOldCode,
			String paNewCode) {
		String vaSql = "SELECT code FROM customer_class WHERE site_id = ? and code != ? and code=?";
		Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paOldCode,
				paNewCode);
		if (result != null) {
			return (String) result.get("CODE");
		} else {
			return null;
		}
	}

}
